package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.ProcessConfigKeyConst;
import com.hyzx.qbasic.common.util.TreeNode;
import com.hyzx.qbasic.admin.model.po.CmsChannelInfoPO;
import com.hyzx.qbasic.admin.model.po.CmsChannelPO;
import com.hyzx.qbasic.admin.model.po.ProcessConfigPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.AddCmsChannelReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteCmsChannelReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.EditCmsChannelReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetCmsChannelListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.common.util.TreeNodeUtils;
import com.hyzx.qbasic.admin.domain.repository.ICmsChannelRepository;
import com.hyzx.qbasic.admin.domain.repository.IProcessConfigRepository;
import com.hyzx.qbasic.admin.domain.service.ICmsChannelService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 黄江华 on 2018/7/24
 */

@Service
public class CmsChannelServiceImpl implements ICmsChannelService {
    @Autowired
    private ICmsChannelRepository channelRepository;

    @Autowired
    private IProcessConfigRepository processConfigRepository;

    @Override
    public PageInfo<CmsChannelInfoPO> getPagedCmsChannelList(GetCmsChannelListReq req) {
        PageInfo<CmsChannelInfoPO> channelList = channelRepository.getPagedCmsChannelList(
                req.getPageNum(), req.getPageSize(), req.getChannelName(), req.getChannelId());

        return channelList;
    }

    @Override
    public BaseOperationRes addCmsChannel(AddCmsChannelReq req) {
        CmsChannelPO channelPO = new CmsChannelPO();
        channelPO.setChannelName(req.getChannelName());
        channelPO.setChannelPath(req.getChannelPath());
        channelPO.setPriority(req.getPriority());
        channelPO.setParentId(req.getParentIdList().get(req.getParentIdList().size() - 1));
        channelPO.setCreateTime(new Date());
        channelPO.setUpdateTime(new Date());

        boolean success = channelRepository.addCmsChannel(channelPO);
        if (!success)
            return OperationResUtils.fail("新增栏目失败");

        //添加审批流程配置
        ProcessConfigPO processConfig = new ProcessConfigPO();
        processConfig.setProcessConfigKey(ProcessConfigKeyConst.PROCESS_CONFIG_KEY_CMS + channelPO.getChannelId());
        processConfig.setProcessCategoryId(req.getProcessCategoryId());
        processConfig.setProcessDefinitionId(req.getProcessDefinitionId());
        processConfig.setDescription("内容发布" + channelPO.getChannelName() + "栏目多级审核配置");
        processConfig.setCreateTime(new Date());
        processConfig.setUpdateTime(new Date());

        processConfigRepository.addProcessConfig(processConfig);

        return OperationResUtils.success();
    }

    @Override
    public List<CmsChannelPO> getAllCmsChannelList() {
        List<CmsChannelPO> poList = channelRepository.getAllCmsChannelList();
        if (CollectionUtils.isEmpty(poList))
            return Collections.emptyList();

        return poList;
    }

    @Override
    public BaseOperationRes deleteCmsChannel(DeleteCmsChannelReq req) {
        List<CmsChannelPO> channelList = channelRepository.getAllCmsChannelList();
        List<TreeNode> treeNodeList = channelList.stream()
                .map(channel -> new TreeNode(channel.getChannelId(), channel.getChannelName(), channel.getParentId()))
                .collect(Collectors.toList());

        List<Integer> childList = new ArrayList<>(channelList.size());

        for (Integer channelId : req.getChannelIdList()) {
            childList.addAll(TreeNodeUtils.getChildNodes(treeNodeList, channelId));
        }

        List<Integer> channelIdList = childList.stream().distinct().collect(Collectors.toList());
        boolean success = channelRepository.deleteCmsChannel(channelIdList);
        if (!success)
            return OperationResUtils.fail("删除栏目失败");

        //删除审批流程配置
        List<String> processConfigKeyList = channelIdList.stream()
                .map(channelId -> ProcessConfigKeyConst.PROCESS_CONFIG_KEY_CMS + channelId)
                .collect(Collectors.toList());
        processConfigRepository.deleteProcessConfigByKey(processConfigKeyList);

        return OperationResUtils.success();
    }

    @Override
    public CmsChannelPO getCmsChannelInfo(int channelId) {
        return channelRepository.getCmsChannelInfo(channelId);
    }

    @Override
    public BaseOperationRes editCmsChannel(EditCmsChannelReq req) {
        //检查栏目是否存在
        CmsChannelPO existedCmsChannel = channelRepository.getCmsChannelInfo(req.getChannelId());
        if (existedCmsChannel == null)
            return OperationResUtils.fail("栏目不存在");

        CmsChannelPO channelPO = new CmsChannelPO();
        channelPO.setChannelId(req.getChannelId());
        channelPO.setChannelName(req.getChannelName());
        channelPO.setChannelPath(req.getChannelPath());
        channelPO.setPriority(req.getPriority());
        channelPO.setParentId(req.getParentIdList().get(req.getParentIdList().size() - 1));
        channelPO.setUpdateTime(new Date());

        boolean success = channelRepository.editCmsChannel(channelPO);
        if (!success)
            return OperationResUtils.fail("编辑栏目信息失败");

        //修改审批流程配置
        String processConfigKey = ProcessConfigKeyConst.PROCESS_CONFIG_KEY_CMS + channelPO.getChannelId();
        ProcessConfigPO processConfig = processConfigRepository.getProcessConfigByKey(processConfigKey);

        if (processConfig != null) {
            processConfig.setProcessDefinitionId(req.getProcessDefinitionId());
            processConfig.setDescription("内容发布" + channelPO.getChannelName() + "栏目多级审核配置");
            processConfig.setUpdateTime(new Date());

            processConfigRepository.editProcessConfig(processConfig);
        }


        return OperationResUtils.success();
    }
}
